/*
 * @Author: c
 * @Date: 2023-12-06 01:06:29
 * @LastEditTime: 2024-01-06 16:53:11
 * @LastEditors: c
 * @Description: 
 * @FilePath: \music-web-backend\src\core\roles\entities\role.entity.ts
 * https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Menu } from "src/core/system/menus/entities/menu.entity";
import { User } from "src/core/system/user/entities/user.entity";
import { BeforeInsert, Column, Entity, JoinTable, ManyToMany, PrimaryGeneratedColumn } from "typeorm";

@Entity()
export class Role {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        type: 'varchar',
        length: 64,
        default: '',
        comment: "角色描述"
    })
    name: string; //角色描述

    @Column({
        type: 'varchar',
        length: 32,
        default: null,
        nullable: true,
        comment: "角色编号"
    })
    code: string; //角色编号

    @Column({
        type: 'int',
        default: null,
        nullable: true,
        comment: "排序"
    })
    sort: number; //排序

    @Column({
        type: 'tinyint',
        default: 1,
        nullable: true,
        comment: "角色状态(1-正常；0-停用)"
    })
    status: number; //状态

    @Column({
        type: 'tinyint',
        default: 0,
        comment: "逻辑删除标识(0-未删除；1-已删除)"
    })
    deleted: number; //是否软删除

    @Column({
        name: 'create_time',
        type: 'timestamp',
        nullable:true,
        default: () => 'CURRENT_TIMESTAMP',
    })
    createTime: Date; //用户创建时间

    @Column({
        name: 'update_time',
        type: 'timestamp',
        nullable:true,
        default: () => 'CURRENT_TIMESTAMP',
    })
    updateTime: Date; //用户信息更新时间

    @ManyToMany(() => User, (user) => user.roles)
    users: User[]

    @ManyToMany(() => Menu, (menu) => menu.role)
    @JoinTable({
        name: "role_menu",
        joinColumn: {
            name: 'role_id',  //生成的列名称
            referencedColumnName: 'id' //关联的原始列名称
        },
        inverseJoinColumn: {
            name: "menu_id",
            referencedColumnName: "id"
        }
    })
    menus: Menu[]

    @BeforeInsert()
    tsoUpperCase(){
       this.code =  this.code.toUpperCase()
    }

    
    
}
